package com.example.charlotte.snakegame;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;


public class MainActivity extends BaseActivity implements View.OnClickListener {
    private ImageButton button1, button2, button3, button4;
    private TextView userName;
    private ToggleButton sound;
    private GameMusic music;
    private SharedPreferences.Editor editor;
    private SharedPreferences saved;
    private boolean bgMusic;
    private boolean isLogin;
    private String name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        music = new GameMusic(this);
        saved = PreferenceManager.getDefaultSharedPreferences(this);
        button1 = findViewById(R.id.button_start);
        button2 = findViewById(R.id.button_rank);
        button3 = findViewById(R.id.button_about);
        button4 = findViewById(R.id.button_setting);
        userName = findViewById(R.id.userName);
        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
        button3.setOnClickListener(this);
        button4.setOnClickListener(this);
        bgMusic = saved.getBoolean("bgmusic", true);
        AutoLogin();
        playBGM();

    }

    @Override
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.button_about:
                intent = new Intent(MainActivity.this, AboutActivity.class);
                break;
            case R.id.button_setting:
                intent = new Intent(MainActivity.this, SettingActivity.class);
                break;
            case R.id.button_start:
                intent = new Intent(MainActivity.this, ChoiceActivity.class);
                break;
            case R.id.button_rank:
                intent = new Intent(MainActivity.this, RankingActivity.class);
                break;

        }
        music.startMusic(3);
        startActivity(intent);

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        stopBGM();

    }

    @Override
    protected void onResume() {
        super.onResume();
        playBGM();

    }

    public void login(View view) {
        music.startMusic(3);
        Intent intent;
        if (isLogin) {
            intent = new Intent(MainActivity.this, PersonInfoActivity.class);
        }else {
            intent = new Intent(MainActivity.this, LoginActivity.class);
        }
        startActivity(intent);
    }


    private void AutoLogin() {
        isLogin = saved.getBoolean("isLogin", false);
        name = saved.getString("name", "");

        if (isLogin) {
            userName.setText(name);
        }
    }
    @Override
    public void onBackPressed() {
        Toast.makeText(this, "dddd", Toast.LENGTH_SHORT).show();
    }
}





